package com.bookstore.service.impl;

import com.bookstore.dao.ProductDao;
import com.bookstore.entity.Product;
import com.bookstore.factory.DaoFactory;
import com.bookstore.service.ProductService;
import com.bookstore.utils.DataDictionary;
import com.bookstore.utils.GudgeType;
import sun.util.calendar.Gregorian;

import java.util.List;

public class ProductServiceImpl implements ProductService {
    ProductDao productDao = DaoFactory.getProductDao();

    @Override
    public List<Product> queryAllProduct() {
        return productDao.queryAllProduct();
    }

    @Override
    public int deleteProduct(Integer id) {
        if (GudgeType.isNull(id)) {
            return DataDictionary.DATAISNULL;
        }
        Product product = queryProductById(id);
        if (product == null) {
            return DataDictionary.DATAISNULL;
        }
        return productDao.deleteProduct(id);
    }

    @Override
    public Product queryProductById(Integer id) {
        if (GudgeType.isNull(id)) {
            return (Product) DataDictionary.SQLQUERYERROR;
        }
        return productDao.queryProductId(id);
    }

    @Override
    public int updateProduct(Product product) {
        if (GudgeType.isNull(product.getId())) {
            return DataDictionary.DATAISNULL;
        }
        return productDao.updateProduct(product);
    }

    @Override
    public int insertProduct(Product product) {
        return productDao.insertProduct(product);
    }

    @Override
    public List<Product> queryLimiteProduct(Integer start,Integer maxNum) {
        if (GudgeType.isNull(start,maxNum)){
            return (List<Product>)DataDictionary.SQLQUERYERROR;
        }
        int count = queryProductCount();
        if (start>=count){
            return (List<Product>)DataDictionary.SQLQUERYERROR;
        }
        return productDao.queryLimiteProduct(start,maxNum);
    }

    @Override
    public int queryProductCount() {
        return productDao.queryProductCount();
    }
}
